jQuery(document).ready(function() {    
    
    jQuery.blockUI.defaults.message = "Cargando...";
    jQuery.blockUI.defaults.css.border = 'none'; 
    jQuery.blockUI.defaults.css.padding = '15px'; 
    jQuery.blockUI.defaults.css.backgroundColor = '#000'; 
    jQuery.blockUI.defaults.css.opacity = '.5'; 
    jQuery.blockUI.defaults.css.color = '#fff';      
    jQuery(document).ajaxStart(jQuery.blockUI).ajaxStop(jQuery.unblockUI);
     
    var split = location.search.replace('?', '').split('=')
    if(split[0] == "post"){
        ajaxCall(split[1]);
    }

	//Eventos de formulario
    jQuery(".wpcf7-form input[name='your-name']").attr('value','Nombre');
    jQuery(".wpcf7-form input[name='your-email']").attr('value','Email');
    jQuery(".wpcf7-form input[name='your-subject']").attr('value','Asunto');
    jQuery(".wpcf7-form textarea[name='your-message']").attr('value','Mensaje');
    
    jQuery(".wpcf7-form input[name='your-name']").focusin(function() {
        var inputValue = jQuery(this).attr('value');
        if(inputValue == 'Nombre') jQuery(this).attr('value','');
        
    });
    jQuery(".wpcf7-form input[name='your-name']").focusout(function() {
        var inputValue = jQuery(this).attr('value');
        if(inputValue == '') jQuery(this).attr('value','Nombre');
    });
    
    jQuery(".wpcf7-form input[name='your-email']").focusin(function() {
        var inputValue = jQuery(this).attr('value');
        if(inputValue == 'Email') jQuery(this).attr('value','');
        
    });
    jQuery(".wpcf7-form input[name='your-email']").focusout(function() {
        var inputValue = jQuery(this).attr('value');
        if(inputValue == '') jQuery(this).attr('value','Email');
    });
    
    jQuery(".wpcf7-form input[name='your-subject']").focusin(function() {
        var inputValue = jQuery(this).attr('value');
        if(inputValue == 'Asunto') jQuery(this).attr('value','');
        
    });
    jQuery(".wpcf7-form input[name='your-subject']").focusout(function() {
        var inputValue = jQuery(this).attr('value');
        if(inputValue == '') jQuery(this).attr('value','Asunto');
    });
    
    jQuery(".wpcf7-form textarea[name='your-message']").focusin(function() {
        var inputValue = jQuery(this).attr('value');
        if(inputValue == 'Mensaje') jQuery(this).attr('value','');
        
    });
    jQuery(".wpcf7-form textarea[name='your-message']").focusout(function() {
        var inputValue = jQuery(this).attr('value');
        if(inputValue == '') jQuery(this).attr('value','Mensaje');
    });
    
	//Eventos de lightbox
    jQuery("#post-lightbox-close").click(function() {
        jQuery("#post-lightbox").fadeOut(500);
    });
    
    jQuery("#post-lightbox-close").mouseover(function() {
        jQuery(this).css("opacity","0.8");
    });
    jQuery("#post-lightbox-close").mouseout(function() {
        jQuery(this).css("opacity","1");
    });
    
    jQuery("#post-lightbox-overlay").click(function() {
        jQuery("#post-lightbox").fadeOut(500);
    });
    
    jQuery("#post-lightbox-previous-post").click(function() {
        var pid = jQuery(this).parent().find("#prev-post-link").html();
        ajaxCall(pid);
    });
    
    jQuery("#post-lightbox-previous-post").mouseover(function() {
        jQuery(this).css("opacity","0.8");
    });
    jQuery("#post-lightbox-previous-post").mouseout(function() {
        jQuery(this).css("opacity","1");
    });
    
    jQuery("#post-lightbox-next-post").click(function() {
        var pid = jQuery(this).parent().find("#next-post-link").html();
        ajaxCall(pid);
    });
    
    jQuery("#post-lightbox-next-post").mouseover(function() {
        jQuery(this).css("opacity","0.8");
    });
    jQuery("#post-lightbox-next-post").mouseout(function() {
        jQuery(this).css("opacity","1");
    });
    
});


jQuery(document).on("mouseover", ".post .media", function() {
    jQuery(this).find('img:first').animate({opacity: 0},500);
});

jQuery(document).on("mouseout", ".post .media", function() {
    jQuery(this).find('img:first').animate({opacity: 1},500);
});

jQuery(document).on("click", ".post .media", function(e) {
	e.preventDefault();
    var pid = jQuery(this).parent(".post").attr("id");
    var arr = pid.split("-");
	ajaxCall(arr[1]);    
});
    
function ajaxCall(pid){
    jQuery.ajax({
            type       : "GET",
            data       : {postId : pid},
            dataType   : "html",
            url        : "http://laplazitalima.com/wp/wp-content/themes/biznetbox/ajaxLoopHandler.php",
            //url        : "http://localhost/plazita/wp-content/themes/biznetbox/ajaxLoopHandler.php",
            success    : function(data){
                $data = jQuery(data);
                jQuery("#post-lightbox-content").html($data);
                jQuery("#post-lightbox-container").center();
                if (stButtons){stButtons.locateElements();}
                jQuery("#post-lightbox").fadeIn(500);
            },
            error     : function(jqXHR, textStatus, errorThrown) {
                //alert(jqXHR + " :: " + textStatus + " :: " + errorThrown);
            }
    });   
}

jQuery.fn.center = function () {
    //this.css("position","absolute");
    
    var width = jQuery(window).width();
    var factor = 0.5;
    if (width <= 480){
        factor = 0.9;
    } else if (width <= 720){
        factor = 0.8;
    }
    
    var elementWidth = factor * width;
    
    this.css("width", elementWidth + "px");
    this.css("margin-left", "-" + (elementWidth/2) + "px");
                                   
    return this;
}